<?php

namespace V1\Model;


use Think\Model;

class QuestionAdoptModel extends Model
{
    protected $tableName = 'question_adopt';

    /**
     * 获取问题被采纳数量
     * @param $userid
     * @return mixed
     */
    public function get_grade($userid)
    {
        $model = M('QuestionAdopt');
        $map['from_uid'] = $userid;
        $data = $model->where($map)->count();
        return $data;
    }

    /**
     * 判断问题是否已经采纳
     * @param $askid [问题id]
     */
    public function check_answer_adopt($askid)
    {
        $count = $this->where(array('askid' => $askid))->count();
        return $count;
    }

    /**
     * 添加问题采纳
     * @param $info [问题、个人信息数组]
     * @return bool
     */
    public function add_answer_adopt($info)
    {
        $data = [
            'askid' => $info['askid'],
            'answerid' => $info['answerid'],
            'to_uid' => $info['to_uid'],
            'from_uid' => $info['from_uid'],
            'addtime' => time(),
        ];
        $request = $this->data($data)->add();
        return $request ? true : false;
    }

    /**
     * 通过问题ID、回复ID查询是否被采纳
     * @param $askid [问题ID]
     * @param $answerid [回复ID]
     * @return mixed
     */
    public function get_answer_adopt_status($askid, $answerid = null)
    {
        $info['askid'] = $askid;
        if (!empty($answerid)) {
            $info['answerid'] = $answerid;
        }
        $data = $this->where($info)->select();
        return $data;
    }
}